<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Db;

/**
 * 升级会员订单管理
 *
 * @icon fa fa-circle-o
 */
class Logistics extends Backend
{

    /**
     * Logistics模型对象
     * @var \app\admin\model\Logistics
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Logistics;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $list = $this->model
                    ->with(['user','gift'])
                    ->where($where)
                    ->where('logistics.status','eq','paid')
                    ->order($sort, $order)
                    ->paginate($limit)->each(function ($item,$key){
                        if($item['gift_status']==0){
                            $item['gift_status_name'] = '未发货';
                        }else{
                            $item['gift_status_name'] = '已发货';
                        }
                        if($item['is_dk']==0){
                            $item['is_dk_name'] = '否';
                        }else{
                            $dkinfo = Db::name('user')->where(array())->find();
                            $item['is_dk_name'] = '是('.$dkinfo['mobile'].')';
                        }
                        if($item['fahuotype']==0){
                            $item['fahuotype_name'] = '邮寄';
                        }else{
                            $item['fahuotype_name'] = '自提';
                        }
                        if(!empty($item['deviceid'])){
                            $item['fahuotype_name']=$item['fahuotype_name'].'-设备号:'.$item['deviceid'];
                        }
                });

            foreach ($list as $row) {
                $row->visible(['id','orderid','amount','balance','user_id','gid','gift_id','gift_status','name','mobile','address','gift_status','gift_status_name','fahuotype','fahuotype_name','express_name','express_no','is_ka','deviceid','paytime','is_dk','is_dk_name','dk_mobile']);
                $row->visible(['user']);
				$row->getRelation('user')->visible(['nickname']);
				$row->visible(['gift']);
				$row->getRelation('gift')->visible(['title']);
            }

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    //编辑
    public function fahuo($ids = null){
        $row = $this->model->get(['id' => $ids]);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a", [], 'strip_tags');
            $params['gift_status'] = 1;
            /*if($params['gift_id']==5 || $params['gift_id']==6){//赠送手表*/
                //查询设备号是否存在
                $watchItem = Db::name('watch_setting')->where(array('deviceid'=>$params['deviceid']))->find();
                if(empty($watchItem)){
                    $this->error('该设备号未录入设备表,请先录入');
                }else{
                    if($watchItem['is_pay'] == 1){
                        $this->error('该设备号已开通,请勿重复开通');
                    }
                    $data['kt_type'] = 1;//开通时光会员送手表
                    $data['is_pay'] = 1;
                    $data['is_ka']=$row['is_ka'];
                    if($row['is_ka']==1){//有卡
                        $data['ka_endtime'] = $params['paytime']+31536000 ;//话费到期时间
                    }
                    Db::name('watch_setting')->where(array('deviceid'=>$params['deviceid']))->update($data);
                }
            /*}*/
            if ($params) {
                $result = $row->save($params);
                if ($result === false) {

                    $this->error($row->getError());
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

}
